package pe.edu.upc.xcode.food.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import pe.edu.upc.xcode.food.domain.Usuario;
import pe.edu.upc.xcode.food.service.UsuarioService;

@Controller
public class LoginController {

	
	@Autowired
	private UsuarioService usuarioService;
	
	public LoginController() {
		// TODO Auto-generated constructor stub
	}
	
	@RequestMapping("/main/home.htm")
	public ModelAndView initial(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		return new ModelAndView("main/home");
	}
	
	
	@RequestMapping("/login/registrar.htm")
	public ModelAndView registrar(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		return new ModelAndView("login/regusuario");
	}
	
	@RequestMapping("/login/guardar.htm")
	public ModelAndView guardar(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		
		String view = "main/home";
		
		String inputNombres = request.getParameter("inputNombres");
		String inputApellidos = request.getParameter("inputApellidos");
		String inputCorreo = request.getParameter("inputCorreo");
		String inputContrasena = request.getParameter("inputContrasena");
		String inputRepContrasena = request.getParameter("inputRepContrasena");
		String optionsCategory = request.getParameter("optionsCategory");
		

		
		System.out.println(optionsCategory);
		
		usuarioService.save(inputNombres, inputApellidos, inputContrasena, inputCorreo, optionsCategory);
		
		
		if(optionsCategory.equals("2")){
			
			view = "restaurant/regrestaurant";
		}
		
		Usuario usuario = usuarioService.autenticar(inputCorreo, inputContrasena);
		
		if (usuario != null) {
			HttpSession session = request.getSession(true);
			session.setAttribute("usuario", usuario);
			session.setAttribute("login", "true");
		}
		
		
		return new ModelAndView(view);
	}
	
	@RequestMapping("/login/autenticar.htm")
	public ModelAndView autenticar(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String inputUsuario = request.getParameter("inputUsuario");
		String inputPwd = request.getParameter("inputPwd");

		Usuario usuario = usuarioService.autenticar(inputUsuario, inputPwd);

		if (usuario != null) {
			HttpSession session = request.getSession(true);
			session.setAttribute("usuario", usuario);
			session.setAttribute("login", "true");
		}

		return new ModelAndView("main/home");
		} 
	
	@RequestMapping("/login/salir.htm")
	public ModelAndView salir(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		HttpSession session = request.getSession();
		
		session.setAttribute("usuario", null);
		session.setAttribute("login", "");
		
		return new ModelAndView("main/home");
	}
}
